package com.treasure.service.collocate.service.impl;

import com.alibaba.fastjson.JSON;
import com.treasure.service.BaseService;
import com.treasure.service.collocate.service.BankConfigService;
import com.treasure.service.commons.lang.util.StringUtil;
import com.treasure.service.domain.TCreditCard;
import com.treasure.service.dto.TBankConfigDto;
import com.treasure.service.dto.TCreditCardDto;
import com.treasure.service.result.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.List;


/**
 * 银行或渠道商配置信息
 *
 * @author <a href="mailto:panxinwen@yingu.com">Pan Xinwen</a>
 * @version 1.0 2018-10-30 11:06
 */
@Service
public class BankConfigServiceImpl extends BaseService implements BankConfigService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public PageResult<List<TBankConfigDto>> getBankConfigList(Integer pageSize, Integer pageNum,Integer recommend,Integer status) {

        MultiValueMap<String, Object> map = new LinkedMultiValueMap();
        map.set("pageSize",pageSize+"");
        map.set("pageNum",pageNum+"");

        if (!StringUtil.isEmpty(recommend)){
            map.set("recommend",recommend+"");
        }
        if (!StringUtil.isEmpty(status)){
            map.set("status",status+"");
        }


        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(map, httpHeaders);
        ParameterizedTypeReference<PageResult<List<TBankConfigDto>>> pt = new ParameterizedTypeReference<PageResult<List<TBankConfigDto>>>() {
        };
        try {
            ResponseEntity<PageResult<List<TBankConfigDto>>> resp = restTemplate.exchange(this.getGateway() + "/bankConfig/v1/getBankConfigList",
                    HttpMethod.POST, formEntity, pt);
            return resp.getBody();
        } catch (HttpClientErrorException e) {
            logger.error("请求方法 "+this.getGateway() + "/bankConfig/v1/getBankConfigList statusCode : {} error:{} ", e.getStatusCode(), e);
        }

        return null;
    }

    @Override
    public PageResult<List<TCreditCardDto>> getCreditList(Integer pageSize, Integer pageNum, Integer status) {

        MultiValueMap<String, Object> map = new LinkedMultiValueMap();
        map.set("pageSize",pageSize);
        map.set("pageNum",pageNum);

        if (!StringUtil.isEmpty(status)){
            map.set("status",status);
        }


        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(map, httpHeaders);
        ParameterizedTypeReference<PageResult<List<TCreditCardDto>>> pt = new ParameterizedTypeReference<PageResult<List<TCreditCardDto>>>() {
        };
        try {
            logger.info("请求地址：{}，参数{}",this.getGateway() + "/bankConfig/v1/getCreditList", JSON.toJSONString(map));
            ResponseEntity<PageResult<List<TCreditCardDto>>> resp = restTemplate.exchange(this.getGateway() + "/bankConfig/v1/getCreditList",
                    HttpMethod.POST, formEntity, pt);
            return resp.getBody();
        } catch (HttpClientErrorException e) {
            logger.error("请求方法 "+this.getGateway() + "/bankConfig/v1/getCreditList statusCode : {} error:{} ", e.getStatusCode(), e);
        }

        return null;
    }
}
